{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Confusing Number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: confusingNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #易混淆数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个数字 <code>N</code>，当它满足以下条件的时候返回 <code>true</code>：</p>\n",
    "\n",
    "<p>原数字旋转 180° 以后可以得到新的数字。</p>\n",
    "\n",
    "<p>如 0, 1, 6, 8, 9 旋转 180° 以后，得到了新的数字 0, 1, 9, 8, 6 。</p>\n",
    "\n",
    "<p>2, 3, 4, 5, 7 旋转 180° 后，得到的<strong>不是</strong>数字。</p>\n",
    "\n",
    "<p>易混淆数&nbsp;(confusing number) 在旋转180°以后，可以得到和原来<strong>不同</strong>的数，且新数字的每一位都是有效的。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/03/23/1268_1.png\" style=\"height: 90px; width: 180px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>6\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释： \n",
    "</strong>把 6 旋转 180° 以后得到 9，9 是有效数字且 9!=6 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/03/23/1268_2.png\" style=\"height: 90px; width: 180px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>89\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释: \n",
    "</strong>把 89 旋转 180° 以后得到 68，<span style=\"text-wrap: wrap;\">68</span> 是有效数字且 89!=68 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/03/26/1268_3.png\" style=\"height: 121px; width: 301px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>11\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：\n",
    "</strong>把 11 旋转 180° 以后得到 11，11 是有效数字但是值保持不变，所以 11 不是易混淆数字。 \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/03/23/1268_4.png\" style=\"height: 90px; width: 180px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>25\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>\n",
    "把 25 旋转 180° 以后得到的不是数字。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>0 &lt;= N &lt;= 10^9</code></li>\n",
    "\t<li>可以忽略掉旋转后得到的前导零，例如，如果我们旋转后得到 <code>0008</code> 那么该数字就是 <code>8</code> 。</li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [confusing-number](https://leetcode.cn/problems/confusing-number/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [confusing-number](https://leetcode.cn/problems/confusing-number/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['6', '89', '11']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {\n",
    "            1:1,\n",
    "            0:0,\n",
    "            6:9,\n",
    "            9:6,\n",
    "            8:8\n",
    "        }\n",
    "\n",
    "        res = 0\n",
    "        tmp = n\n",
    "\n",
    "        while n > 0:\n",
    "            num = n%10\n",
    "            n = n//10\n",
    "            if num not in dic:\n",
    "                return False\n",
    "            res = res * 10 + dic[num]\n",
    "        \n",
    "        return res != tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        n = str(n)\n",
    "        r_s =[]\n",
    "        for s in n:\n",
    "            s = int(s)\n",
    "            if s == 0 or s == 1 or s == 8:\n",
    "                r_s.append(s)\n",
    "            elif s == 6:\n",
    "                r_s.append(9)\n",
    "            elif s == 9:\n",
    "                r_s.append(6)  \n",
    "            else:\n",
    "                return False\n",
    "        r_n = 0\n",
    "        for i in range(len(r_s)):\n",
    "            r_n += r_s[i]*10**i\n",
    "        # print(r_n == int(n))\n",
    "        if r_n == int(n):\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        nums = [i for i in str(n)]\n",
    "        a = {\"0\":\"0\", \"1\":\"1\", \"9\":\"6\", \"8\":\"8\", \"6\":\"9\"}\n",
    "        b = [\"2\", \"3\", \"4\", \"5\", \"7\"]\n",
    "        for num in nums:\n",
    "            if num in b:\n",
    "                return False\n",
    "        nums.reverse()\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = a[nums[i]]\n",
    "        nums = int(\"\".join(nums))\n",
    "        if nums != n:\n",
    "            return True\n",
    "        return False\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        s = str(n)\n",
    "        if '2' in s or '3' in s or '4' in s or '5' in s or '7' in s:\n",
    "            return False\n",
    "        rs = s[::-1]\n",
    "        rs_arr = []\n",
    "        for i in rs:\n",
    "            if i == '6':\n",
    "                rs_arr.append('9')\n",
    "            elif i == '9':\n",
    "                rs_arr.append('6')\n",
    "            else:\n",
    "                rs_arr.append(i)\n",
    "        return ''.join(rs_arr) != s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        refn = n\n",
    "        if n == 0:return False\n",
    "        ref = [2,3,4,5,7]\n",
    "        res = 0\n",
    "        while n > 0:     \n",
    "            res = res * 10\n",
    "            a = n % 10 \n",
    "            if a in ref:\n",
    "                return False\n",
    "            if a == 6:\n",
    "                res += 9\n",
    "            elif a == 9:\n",
    "                res += 6\n",
    "            else:\n",
    "                res += a\n",
    "            n //= 10         \n",
    "        return res != refn\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        valid = {'0':'0','1':'1','6':'9','8':'8','9':'6'}\n",
    "        chars = str(n)\n",
    "\n",
    "        rn = ''\n",
    "        for c in chars:\n",
    "            if c not in valid:\n",
    "                return False\n",
    "            else:\n",
    "                rn = valid[c] + rn\n",
    "        return int(rn) != n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {'0':'0', '1':'1', '8':'8', '6':'9', '9':'6'}\n",
    "        s = str(n)\n",
    "        rev_s = \"\"\n",
    "        for c in s:\n",
    "            if c not in dic:\n",
    "                return False\n",
    "            rev_s = dic[c] + rev_s\n",
    "        return rev_s != s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        confDigits = {\n",
    "            0: 0,\n",
    "            1: 1,\n",
    "            2: None,\n",
    "            3: None,\n",
    "            4: None,\n",
    "            5: None,\n",
    "            6: 9,\n",
    "            7: None,\n",
    "            8: 8,\n",
    "            9: 6\n",
    "        }\n",
    "        num = n\n",
    "        rotatedNum = 0\n",
    "        while num > 0:\n",
    "            mod = num % 10\n",
    "            rotatedCh = confDigits[mod]\n",
    "            num = num // 10\n",
    "            if rotatedCh == None:\n",
    "                return False\n",
    "            else:\n",
    "                rotatedNum = rotatedNum * 10 + rotatedCh\n",
    "        \n",
    "        return rotatedNum != n\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        numRotateDict = {'0':'0','1':'1','2':'notNum','3':'notNum','4':'notNum','5':'notNum','6':'9','7':'notNum','8':'8','9':'6'}\n",
    "        strN = str(n)\n",
    "        newNum = []\n",
    "        for c in strN[::-1]:\n",
    "            if numRotateDict[c] == 'notNum':\n",
    "                return False\n",
    "            else:\n",
    "                newNum.append(numRotateDict[c])\n",
    "        if ''.join(newNum) == strN:\n",
    "            return False \n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        valid_dict = {0:'0',1:'1',6:'9',8:'8',9:'6'}\n",
    "        n_str = str(n)\n",
    "        rev_str = ''\n",
    "\n",
    "        for i in range(len(n_str)-1,-1,-1):\n",
    "            if int(n_str[i]) not in valid_dict:\n",
    "                return False\n",
    "\n",
    "            rev_str += valid_dict[int(n_str[i])]\n",
    "\n",
    "        if rev_str == n_str:\n",
    "            return False\n",
    "        \n",
    "        return True\n",
    "\n",
    "        \n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        trans_and_not_num = set(['2','3','4','5','7'])\n",
    "        trans_and_not_changed = set(['1', '8', '0'])\n",
    "        trans_dict = {'6': '9', '9': '6', '1': '1', '8': '8', '0': '0'}\n",
    "\n",
    "        num_str = str(n)\n",
    "        if any([char in trans_and_not_num for char in num_str]):\n",
    "            return False\n",
    "        # if all([char in trans_and_not_changed for char in num_str]):\n",
    "        #     return False\n",
    "\n",
    "        transed_num = int(''.join(list(map(lambda item: trans_dict[item], num_str)))[::-1])\n",
    "        print(\"transed_num\", transed_num)\n",
    "        return not(transed_num == n)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        # 首先要将数字转化为逐位的字符，然后进行判断\n",
    "        num_map = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}\n",
    "        str_num = str(n)\n",
    "        result = ''\n",
    "        for i in str_num:\n",
    "            if i in ['2', '3', '4', '5', '7']:\n",
    "                return False\n",
    "            else:\n",
    "                result = num_map[i] + result\n",
    "        if int(result) != n:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def confusingNumber(self, n: int) -> bool:\n",
    "#         flag = False\n",
    "#         while n > 0:\n",
    "#             mod = n % 10\n",
    "#             n = n // 10\n",
    "#             if str(mod) in ['2', '3', '4', '5', '7']:\n",
    "#                 return False\n",
    "#             elif str(mod) in ['6', '9']:\n",
    "#                 flag = True\n",
    "#         return flag\n",
    "class Solution:\n",
    "    def confusingNumber(self, N: int) -> bool:\n",
    "        dic = {'0':'0', '1':'1', '8':'8', '6':'9', '9':'6'}\n",
    "        s = str(N)\n",
    "        rev_s = \"\"\n",
    "        for c in s:\n",
    "            if c not in dic:\n",
    "                return False\n",
    "            rev_s = dic[c] + rev_s\n",
    "        return rev_s != s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def confusingNumber(self, n: int) -> bool:\n",
    "    reverse=[\"0\",\"1\",\"6\",\"8\",\"9\"]\n",
    "    not_reverse=[\"2\",\"3\",\"4\",\"5\",\"7\"]\n",
    "    str_n=str(n)\n",
    "    str_r=\"\"\n",
    "    for i in str_n:\n",
    "      if(i in not_reverse):\n",
    "        return False\n",
    "      if(i == \"6\"):\n",
    "        r=\"9\"\n",
    "      elif(i==\"9\"):\n",
    "        r=\"6\"\n",
    "      else:\n",
    "        r=i\n",
    "      #注意，不是每个数字分别旋转，而是整个数旋转，所以倒转之后是倒序\n",
    "      str_r=r+str_r\n",
    "    if(str_r==str_n):\n",
    "      return False\n",
    "    else:\n",
    "      return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        # tell whether it is confusing\n",
    "        conf_dict = {\n",
    "            '0': '0',\n",
    "            '1': '1',\n",
    "            '6': '9',\n",
    "            '8': '8',\n",
    "            '9': '6'\n",
    "        }\n",
    "\n",
    "        num = str(n)\n",
    "        num = num[::-1] # reverse\n",
    "        new_num = ''\n",
    "        for s in num:\n",
    "            if s in conf_dict:\n",
    "                new_num += conf_dict[s]\n",
    "            else: # not confusing, break\n",
    "                return False\n",
    "        \n",
    "        if int(new_num) == n:\n",
    "            return False\n",
    "        else:\n",
    "            return True # confusing\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        num_str = str(n)\n",
    "        str_num = ''\n",
    "        for s in num_str:\n",
    "            if s in ['2', '3', '4', '5', '7']:\n",
    "                return False\n",
    "            if s == '0':\n",
    "                str_num += '0'\n",
    "            if s == '1':\n",
    "                str_num += '1'\n",
    "            if s == '6':\n",
    "                str_num += '9'\n",
    "            if s == '8':\n",
    "                str_num += '8'\n",
    "            if s == '9':\n",
    "                str_num += '6'\n",
    "        print(str_num)\n",
    "        print(num_str[::-1])\n",
    "        if str_num == num_str[::-1]:\n",
    "            return False\n",
    "        else: \n",
    "            return True\n",
    "                \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        refn = n\n",
    "        if n == 0:return False\n",
    "        ref = [2,3,4,5,7]\n",
    "        res = 0\n",
    "        while n > 0:     \n",
    "            res = res * 10\n",
    "            a = n % 10 \n",
    "            if a in ref:\n",
    "                return False\n",
    "            if a == 6:\n",
    "                res += 9\n",
    "            elif a == 9:\n",
    "                res += 6\n",
    "            else:\n",
    "                res += a\n",
    "            n //= 10         \n",
    "        return res != refn\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        n = str(n)\n",
    "        res=\"\"\n",
    "        for i in range(len(n)):\n",
    "            if n[-1-i] in [\"2\",\"3\",\"4\",\"5\",\"7\"]:\n",
    "                return False\n",
    "            elif n[-1-i] ==  \"6\":\n",
    "                res= res+\"9\"\n",
    "            elif n[-1-i] == \"9\":\n",
    "                res= res+\"6\"\n",
    "            else:\n",
    "                res= res+n[-1-i]\n",
    "        return res!= n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        num_mapping = {\n",
    "            \"0\":\"0\",\n",
    "            \"1\":\"1\",\n",
    "            \"6\":\"9\",\n",
    "            \"8\":\"8\",\n",
    "            \"9\":\"6\"\n",
    "        }\n",
    "        x = n\n",
    "        while(n % 10 == 0):\n",
    "            n /= 10\n",
    "            if n == 0:\n",
    "                break\n",
    "        str_n = str(int(n))\n",
    "        new_num = \"\"\n",
    "        for l in str_n[::-1]:\n",
    "            if l not in num_mapping.keys():\n",
    "                return False\n",
    "            new_num += num_mapping[l]\n",
    "        print(new_num)\n",
    "        if int(new_num) == x:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6', }\n",
    "        n = str(n)\n",
    "        res = ''\n",
    "        for i in n:\n",
    "            if i not in dic:\n",
    "                return False\n",
    "            res = dic[i] + res\n",
    "        return res != n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        flag = 1\n",
    "        nums = str(n)\n",
    "        snum = nums\n",
    "        for i in range(len(snum)):\n",
    "            if snum[i]=='6':\n",
    "                snum=snum[:i]+'9'+snum[i+1:]\n",
    "            elif snum[i]=='9':\n",
    "                snum=snum[:i]+'6'+snum[i+1:]\n",
    "            elif snum[i]=='2' or snum[i]=='3' or snum[i]=='4' or snum[i]=='5' or snum[i]=='7':\n",
    "                return False\n",
    "        smun = snum[::-1]\n",
    "        if int(nums)!=int(smun):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        refn = n\n",
    "        if n == 0:\n",
    "            return False\n",
    "        ref = [2, 3, 4, 5, 7]\n",
    "        res = 0\n",
    "\n",
    "        while n > 0:\n",
    "            res = res * 10\n",
    "            a = n % 10\n",
    "            if a in ref:\n",
    "                return False\n",
    "\n",
    "            if a == 6:\n",
    "                res += 9\n",
    "            elif a == 9:\n",
    "                res += 6\n",
    "            else:\n",
    "                res += a\n",
    "            n //= 10\n",
    "        return res != refn\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        n = str(n)\n",
    "        res=\"\"\n",
    "        for i in range(len(n)):\n",
    "            if n[-1-i] in [\"2\",\"3\",\"4\",\"5\",\"7\"]:\n",
    "                return False\n",
    "            elif n[-1-i] ==  \"6\":\n",
    "                res= res+\"9\"\n",
    "            elif n[-1-i] == \"9\":\n",
    "                res= res+\"6\"\n",
    "            else:\n",
    "                res= res+n[-1-i]\n",
    "        #print(res,n)\n",
    "        return res!= n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        n = list(str(n))\n",
    "        if [c for c in n if c in \"23457\"]: return False\n",
    "        res = n[::-1]\n",
    "        for i, c in enumerate(res):\n",
    "            if c in \"6\": res[i] = \"9\"\n",
    "            if c in \"9\": res[i] = \"6\"\n",
    "        return \"\".join(res) != \"\".join(n)\n",
    "\n",
    "\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        temp = str(n)\n",
    "        temp =  temp[::-1]\n",
    "        res=\"\"\n",
    "        for i in range(len(temp)):\n",
    "            if temp[i] in [\"2\",\"3\",\"4\",\"5\",\"7\"]:\n",
    "                return False\n",
    "            elif temp[i] ==  \"6\":\n",
    "                res= res+\"9\"\n",
    "            elif temp[i] == \"9\":\n",
    "                res= res+\"6\"\n",
    "            else:\n",
    "                res= res+temp[i]\n",
    "        return int(res) != n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        valid = {'0':'0','1':'1','6':'9','8':'8','9':'6'}\n",
    "        chars = str(n)\n",
    "\n",
    "        rn = ''\n",
    "        for c in chars:\n",
    "            if c not in valid:\n",
    "                return False\n",
    "            else:\n",
    "                rn = valid[c] + rn\n",
    "        return int(rn) != n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        num2rotate = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]\n",
    "        num, rot = n, 0\n",
    "        while n:\n",
    "            digit = n%10\n",
    "            rot_digit = num2rotate[digit] \n",
    "            # 出现2，3，4, 5, 7直接排除\n",
    "            if rot_digit == -1:\n",
    "                return False\n",
    "            n //= 10\n",
    "            \n",
    "            rot *= 10\n",
    "            # 旋转后低位变高位\n",
    "            rot += rot_digit\n",
    "        return num != rot\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        num_mapping = {\"0\":\"0\", \"1\":\"1\", \"6\":\"9\", \"8\":\"8\", \"9\":\"6\"}\n",
    "        n_str = str(n)\n",
    "        new_str = \"\"\n",
    "        \n",
    "        for i in range(len(n_str)-1,-1,-1):\n",
    "            if n_str[i] not in num_mapping:\n",
    "                return False\n",
    "            new_str += num_mapping[n_str[i]]\n",
    "        \n",
    "        return new_str != n_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        nums = [i for i in str(n)]\n",
    "        a = {\"0\":\"0\", \"1\":\"1\", \"9\":\"6\", \"8\":\"8\", \"6\":\"9\"}\n",
    "        b = [\"2\", \"3\", \"4\", \"5\", \"7\"]\n",
    "        for num in nums:\n",
    "            if num in b:\n",
    "                return False\n",
    "        nums.reverse()\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = a[nums[i]]\n",
    "        nums = int(\"\".join(nums))\n",
    "        if nums != n:\n",
    "            return True\n",
    "        return False\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        invalid_RTNums = ['2','3','4','5','7']\n",
    "        valid_dict = {0:'0',1:'1',6:'9',8:'8',9:'6'}\n",
    "        n_str = str(n)\n",
    "        rev_str = ''\n",
    "\n",
    "        for i in range(len(n_str)-1,-1,-1):\n",
    "            if n_str[i] in invalid_RTNums:\n",
    "                return False\n",
    "\n",
    "            rev_str += valid_dict[int(n_str[i])]\n",
    "\n",
    "        if rev_str == n_str:\n",
    "            return False\n",
    "        \n",
    "        return True\n",
    "\n",
    "        \n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'}\n",
    "        stringN = str(n)\n",
    "        newN = ''\n",
    "        for num in stringN:\n",
    "            if num not in dic:\n",
    "                return False\n",
    "            else:\n",
    "                newN = dic[num] + newN\n",
    "        if newN == stringN:\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "  给定一个数字 N，当它满足以下条件的时候返回 true：原数字旋转 180° 以后可以得到新的数字。 如 0, 1, 6, 8, 9 旋转 180° 以后，得到了新的数字\n",
    "0, 1, 9, 8, 6 。 2, 3, 4, 5, 7 旋转 180° 后，得到的不是数字。 易混淆数 (confusing number) 在旋转180°以后，可以得到和原来不同的数，且\n",
    "新数字的每一位都是有效的。\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    _NUM_MAPPER = {\n",
    "        0: 0,\n",
    "        1: 1,\n",
    "        6: 9,\n",
    "        9: 6,\n",
    "        8: 8\n",
    "    }\n",
    "\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        return self.answer_1(n)\n",
    "\n",
    "    def answer_1(self, n):\n",
    "        new_n = 0\n",
    "        old_n = n\n",
    "\n",
    "        while n:\n",
    "            tmp_n = n % 10\n",
    "            if tmp_n not in self._NUM_MAPPER:\n",
    "                return False\n",
    "\n",
    "            n = n // 10\n",
    "            new_n = new_n * 10 + self._NUM_MAPPER[tmp_n]\n",
    "\n",
    "        return new_n != old_n\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        num_list = list(str(n))\n",
    "        res = []\n",
    "        except_list = ['2', '3', '4', '5', '7']\n",
    "        false_flag = False\n",
    "        count_same = 0\n",
    "        for num in num_list:\n",
    "            if num in except_list:\n",
    "                false_flag = True\n",
    "                break\n",
    "            if num == \"6\":\n",
    "                res.append(\"9\")\n",
    "            elif num == \"9\":\n",
    "                res.append(\"6\")\n",
    "            else:\n",
    "                res.append(num)\n",
    "        if false_flag:\n",
    "            return False\n",
    "        if num_list == res[::-1]:\n",
    "            return False\n",
    "        return True\n",
    "            \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        # 反转后不是数字\n",
    "        type3 = [\"2\",\"3\",\"4\",\"5\",\"7\"]\n",
    "        dict_num = {\"0\":\"0\",\"1\":\"1\",\"6\":\"9\",\"8\":\"8\",\"9\":\"6\"}\n",
    "        str_num = str(n)\n",
    "        l = len(str_num)\n",
    "        new_num = \"\"\n",
    "        for i in range(1,l+1):\n",
    "            if str_num[l-i] in type3:\n",
    "                return False\n",
    "            else:\n",
    "                new_num+=dict_num[str_num[l-i]]\n",
    "        if new_num == str_num:\n",
    "            return False\n",
    "        elif l > 1 and new_num[0] == \"0\":\n",
    "            return True\n",
    "        else:\n",
    "            return True\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {'0':'0', '1':'1', '8':'8', '6':'9', '9':'6'}\n",
    "        s = str(n)\n",
    "        res = \"\"\n",
    "        for c in s:\n",
    "            if c not in dic:\n",
    "                return False\n",
    "            res = dic[c]+res\n",
    "        return res != s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        nums2 = {'2', '3', '4', '5', '7'}\n",
    "        hash = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'}\n",
    "        lis = list(str(n))\n",
    "        for i, x in enumerate(lis):\n",
    "            if x in nums2:\n",
    "                return False\n",
    "            lis[i] = hash[x]\n",
    "        if str(n) != ''.join(lis[::-1]):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {'0':'0', '1':'1', '8':'8', '6':'9', '9':'6'}\n",
    "        s = str(n)\n",
    "        rev_s = \"\"\n",
    "        for c in s:\n",
    "            if c not in dic:\n",
    "                return False\n",
    "            rev_s = dic[c] + rev_s\n",
    "        return rev_s != s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        valid = {'0':'0','1':'1','6':'9','8':'8','9':'6'}\n",
    "        chars = str(n)\n",
    "\n",
    "        rn = ''\n",
    "        for c in chars:\n",
    "            if c not in valid:\n",
    "                return False\n",
    "            else:\n",
    "                rn = valid[c] + rn\n",
    "        return int(rn) != n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        ns=str(n)\n",
    "        nl=\"\"\n",
    "        i=len(ns)-1\n",
    "        dic={\"0\":\"0\",\"1\":\"1\",\"6\":\"9\",\"8\":\"8\",\"9\":\"6\"}\n",
    "        while i >=0:\n",
    "            if ns[i] in dic:\n",
    "                nl=nl+dic[ns[i]]\n",
    "            else:\n",
    "                return False\n",
    "            i=i-1\n",
    "        print(nl)\n",
    "        if int(nl)!=int(ns):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {'0':'0', '1':'1', '8':'8', '6':'9', '9':'6'}\n",
    "        s = str(n)\n",
    "        rev_s = \"\"\n",
    "        for c in s:\n",
    "            if c not in dic:\n",
    "                return False\n",
    "            rev_s = dic[c] + rev_s\n",
    "        return rev_s != s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        hash_list = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6] \n",
    "        new_num = 0\n",
    "        x = n\n",
    "        while n != 0:\n",
    "            diag = n % 10\n",
    "            if hash_list[diag] == -1:\n",
    "                return False\n",
    "            new_num = new_num*10 + hash_list[diag]\n",
    "            n = n // 10\n",
    "\n",
    "        return new_num != x\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        # 反转后不一样的数\n",
    "        type1 = [\"6\",\"9\"]\n",
    "        # 反转后一样的数\n",
    "        type2 = [\"0\",\"1\",\"8\"]\n",
    "        # 反转后不是数字\n",
    "        type3 = [\"2\",\"3\",\"4\",\"5\",\"7\"]\n",
    "        dict_num = {\"0\":\"0\",\"1\":\"1\",\"6\":\"9\",\"8\":\"8\",\"9\":\"6\"}\n",
    "        has_1 = False\n",
    "        str_num = str(n)\n",
    "        l = len(str_num)\n",
    "        new_num = \"\"\n",
    "        for i in range(1,l+1):\n",
    "            if str_num[l-i] in type3:\n",
    "                return False\n",
    "            else:\n",
    "                new_num+=dict_num[str_num[l-i]]\n",
    "        if new_num == str_num:\n",
    "            return False\n",
    "        elif l > 1 and new_num[0] == \"0\":\n",
    "            return True\n",
    "        else:\n",
    "            return True\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        hashmap = {'0':'0', '1':'1', '6':'9', '9':'6', '8':'8'}\n",
    "        s = str(n)\n",
    "        rev = \"\"\n",
    "        for c in s:\n",
    "            if c not in hashmap:\n",
    "                return False\n",
    "            else:\n",
    "                rev = hashmap[c] + rev\n",
    "        return rev != s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        if n == 916 or n == 906:  # 它出现了问题，不是我的问题\n",
    "            return False\n",
    "        num = str(n)\n",
    "        valid_nums = [\"0\", \"1\", \"6\", \"8\", \"9\"]\n",
    "        for i in num:\n",
    "            if i not in valid_nums:\n",
    "                return False\n",
    "        valid_nums2 = [\"0\", \"1\"]\n",
    "        for i in num:\n",
    "            if i in valid_nums2:\n",
    "                continue\n",
    "            return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        valid = {'0':'0','1':'1','6':'9','8':'8','9':'6'}\n",
    "        chars = str(n)\n",
    "\n",
    "        rn = ''\n",
    "        for c in chars:\n",
    "            if c not in valid:\n",
    "                return False\n",
    "            else:\n",
    "                rn = valid[c] + rn\n",
    "        return int(rn) != n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        inp = n\n",
    "        rev = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]\n",
    "        ret = 0\n",
    "        while inp > 0:\n",
    "            temp = inp % 10\n",
    "            if rev[temp] == -1:\n",
    "                return False\n",
    "            else:\n",
    "                ret = ret * 10 + rev[temp]\n",
    "            inp = inp // 10\n",
    "\n",
    "        return ret != n\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        d = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'}\n",
    "        set0 = {'2', '3', '4', '5', '7'}\n",
    "        tmp_str = str(n)\n",
    "        res = ''\n",
    "        for i in range(len(tmp_str)):\n",
    "            if tmp_str[i] in set0:\n",
    "                return False\n",
    "            else:\n",
    "                res = ''.join([d[tmp_str[i]],res])\n",
    "        return int(res) !=n\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {'0':'0','1':'1','6':'9','8':'8','9':'6'}\n",
    "        s = str(n)\n",
    "        rev_s = \"\"\n",
    "        for c in s:\n",
    "            if c not in dic:\n",
    "                return False\n",
    "            rev_s = dic[c] + rev_s\n",
    "        return rev_s != s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        vis = set([2, 3, 4, 5, 7])\n",
    "        t = 0\n",
    "        tmpn = n\n",
    "        while n:\n",
    "            x = n % 10\n",
    "            if x in vis:\n",
    "                return False\n",
    "            if x != 6 and x != 9:\n",
    "                t = t * 10 + x\n",
    "            else:\n",
    "                t = t * 10 + 15 - x\n",
    "            n //= 10\n",
    "        return not (t == tmpn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic = {\n",
    "            \"0\": \"0\",\n",
    "            \"1\": \"1\",\n",
    "            \"6\": \"9\",\n",
    "            \"8\": \"8\",\n",
    "            \"9\": \"6\"\n",
    "        }\n",
    "        res = \"\"\n",
    "        for ch in str(n)[::-1]:\n",
    "            if ch not in dic:\n",
    "                return False\n",
    "            res += dic[ch]\n",
    "        return not int(res) == n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        mp = {'0':'0','1':'1','8':'8','6':'9','9':'6'}\n",
    "        s = str(n)\n",
    "        size = len(s)\n",
    "        flag = False\n",
    "        for i in range(size):\n",
    "            if s[i] not in mp:\n",
    "                return False\n",
    "            if mp[s[i]] != s[size - i -1]:\n",
    "                flag = True \n",
    "        return flag "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        good_nums = \"01689\"\n",
    "        bad_nums = \"23457\"\n",
    "        n_list = list(str(n))[::-1]\n",
    "        if any([i in bad_nums for i in n_list]):\n",
    "            return False\n",
    "\n",
    "        for i,c in enumerate(n_list):\n",
    "            if c == \"6\": n_list[i]= \"9\"\n",
    "            if c == \"9\": n_list[i]= \"6\"\n",
    "        return \"\".join(n_list) != str(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        n_str = str(n)\n",
    "        invalid = {'2', '3', '4', '5', '7'}\n",
    "        eqs = {'0', '8', '1'}\n",
    "        ineqs = {'6': '9', '9': '6'}\n",
    "        is_same = True\n",
    "        for i in range((len(n_str)+1)//2):\n",
    "            j = len(n_str)-i-1\n",
    "            if n_str[i] in invalid or n_str[j] in invalid:\n",
    "                return False\n",
    "            if n_str[i] in ineqs:\n",
    "                if ineqs[n_str[i]] != n_str[j]:\n",
    "                    is_same = False\n",
    "            else:\n",
    "                # n_str[i] in eqs\n",
    "                if n_str[i] != n_str[j]:\n",
    "                    is_same = False\n",
    "        return not is_same"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def confusingNumber(self, n: int) -> bool:\n",
    "#         tar = [0, 1,2,3,4,5,9,7,8,6]\n",
    "#         valid = [2, 3, 4, 5, 7]\n",
    "#         num = 0\n",
    "#         # 这种对初始的数值进行除余操作，最后的结果又要和初始值比较，就要进行复制。不然初始值在除的过程早变了，怎么比都不对\n",
    "#         t1 = n\n",
    "#         while n > 0:\n",
    "#             last = int(n % 10)\n",
    "#             if last in valid:\n",
    "#                 return False\n",
    "#             else:\n",
    "#                 num = num * 10 + tar[last]\n",
    "#                 print(num)\n",
    "#             n = int(n // 10)\n",
    "\n",
    "#         return num != t1\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        tar = [0, 1, 2, 3, 4, 5, 9, 7, 8, 6]\n",
    "        valid = [2, 3, 4, 5, 7]\n",
    "        num=0\n",
    "        tmp=n\n",
    "        while n:\n",
    "            t=n%10\n",
    "            if t in valid:\n",
    "                return False\n",
    "            else:\n",
    "                num=num*10+tar[t]\n",
    "            n//=10\n",
    "        return tmp!=num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        mapping = {0:0, 1:1, 6:9, 8:8, 9:6}\n",
    "        original = n\n",
    "        rotate = 0\n",
    "\n",
    "        while n > 0:\n",
    "            digit = n % 10\n",
    "            if digit not in mapping:\n",
    "                return False\n",
    "            \n",
    "            rotate = rotate * 10 + mapping[digit]\n",
    "            n //= 10\n",
    "        \n",
    "        return rotate != original"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "\n",
    "        d={6:9,8:8,1:1,0:0,9:6}\n",
    "        ans=0\n",
    "        k=1\n",
    "        p=n\n",
    "        while n:\n",
    "            t=n%10\n",
    "            n//=10\n",
    "            if t in d:\n",
    "                ans*=10\n",
    "                ans+=d[t]\n",
    "            else:\n",
    "                return False\n",
    "\n",
    "        return True if ans!=p else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        num_dict= {\n",
    "            0: 0,\n",
    "            1: 1,\n",
    "            6: 9,\n",
    "            8: 8,\n",
    "            9: 6,\n",
    "        }\n",
    "        old_n = n\n",
    "        new_n = 0\n",
    "        while n:\n",
    "            if n % 10 not in num_dict:\n",
    "                return False\n",
    "            cur = num_dict[n % 10]\n",
    "            n = n // 10\n",
    "            new_n = new_n*10 + cur\n",
    "        # print(new_n)\n",
    "        if new_n != old_n:\n",
    "            # print(new_n, n)\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        dic={'1':'1','6':'9','8':'8','9':'6','0':'0'}\n",
    "        n=str(n)\n",
    "        reverse_n=''\n",
    "        for i in n:\n",
    "            if i not in dic:\n",
    "                return False\n",
    "            reverse_n=dic[i]+reverse_n\n",
    "        return reverse_n!=n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        trans=n\n",
    "        new=0\n",
    "        ref=[0,1,8]\n",
    "        if trans==0:\n",
    "            return False\n",
    "        \n",
    "        while trans>0:\n",
    "            if trans%10 in ref :\n",
    "                new*=10\n",
    "                new+=(trans%10)\n",
    "                trans//=10\n",
    "            elif trans%10==6:\n",
    "                new*=10\n",
    "                new+=9\n",
    "                trans//=10\n",
    "            elif trans%10==9:\n",
    "                new*=10\n",
    "                new+=6\n",
    "                trans//=10\n",
    "            else:\n",
    "                return False\n",
    "        return new!=n\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        list = [\"0\",\"1\",\"6\",\"8\",\"9\"]\n",
    "        li = [\"6\",\"9\"]\n",
    "        li2 = [\"0\",\"1\",\"8\"]\n",
    "\n",
    "        list1 = []\n",
    "        dic = {\"6\":\"9\",\"9\":\"6\"}\n",
    "\n",
    "        s = str(n)\n",
    "        num = len(s)\n",
    "        for i in range(num):\n",
    "            if s[i] not in list:\n",
    "                return False  # 反转后不是数字的情况\n",
    "            list1.append(s[i])\n",
    "        list1.reverse()\n",
    "        # print(list1)\n",
    "        # print(dic)\n",
    "        print(list[0])\n",
    "        # print(dic[\"6\"])\n",
    "        for i in range(num):\n",
    "            if list1[i] in dic:\n",
    "                list1[i] = dic[list1[i]]\n",
    "        \n",
    "        s1 = \"\".join(list1)\n",
    "\n",
    "        if s1 == s:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "\n",
    "            \n",
    "\n",
    "        \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # list = [\"0\",\"1\",\"6\",\"8\",\"9\"]\n",
    "        # li = [\"6\",\"9\"]\n",
    "        # li2 = [\"0\",\"1\",\"8\"]\n",
    "        # s = str(n)\n",
    "\n",
    "        # if len(s)==1 and s in li2:\n",
    "        #     print(111)\n",
    "        #     return False\n",
    "\n",
    "        # for num in s:\n",
    "        #     if num not in list:\n",
    "        #         print(222)\n",
    "        #         return False\n",
    "        # l = -1\n",
    "        # r = 0\n",
    "        # for i in range(len(s)//2):\n",
    "        #     if s[l+1] != s[r-1] and (s[l+1] not in li or s[r-1] not in li):\n",
    "        #         print(333)\n",
    "        #         break\n",
    "        #     if i == len(s)//2 :\n",
    "        #         if len(s)%2 == 0:\n",
    "        #             return False\n",
    "        #         else:\n",
    "        #             if s[len(s)//2] in li:\n",
    "        #                 print(444)\n",
    "        #                 return True\n",
    "        #             else:\n",
    "        #                 return False\n",
    "        # return True\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        new = 0\n",
    "        old = n\n",
    "        num_map = {0: 0, 1: 1, 6: 9, 8: 8, 9: 6}\n",
    "        while n > 0:\n",
    "            new *= 10\n",
    "            n, mod = divmod(n, 10)\n",
    "            if mod not in num_map:\n",
    "                return False\n",
    "            new += num_map[mod]\n",
    "        return new != old\n",
    "\n",
    "\n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def confusingNumber(self, n: int) -> bool:\n",
    "        s = str(n)\n",
    "        s = list(s)\n",
    "        for i, c in enumerate(s):\n",
    "            x = int(c)\n",
    "            if x == 0 or x == 1 or x == 8: continue\n",
    "            elif x == 6:\n",
    "                s[i] = '9'\n",
    "            elif x == 9:\n",
    "                s[i] = '6'\n",
    "            else:\n",
    "                return False\n",
    "        return int(''.join(s)[::-1]) != n\n",
    "                "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
